package com.sx.controller;

import com.sx.pojo.Goods;
import com.sx.service.CategoryService;
import com.sx.service.GoodService;
import com.sx.service.SupplierService;
import com.sx.service.serviceImpl.CategoryServiceImpl;
import com.sx.service.serviceImpl.GoodServiceImpl;
import com.sx.service.serviceImpl.SupplierServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet("/goodsSelect")
public class GoodSelect extends HttpServlet {
    private final GoodService goodsService = new GoodServiceImpl();
    private final SupplierService supplierService = new SupplierServiceImpl();
    private final CategoryService categoryService = new CategoryServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        // 获取用户输入的查询条件
        // 从请求中获取用户输入的查询条件
        String categoryId = request.getParameter("categoryId");
        String numberOrder = request.getParameter("numberOrder");
        String shelfLifeOrder = request.getParameter("shelfLifeOrder");
        String priceOrder = request.getParameter("priceOrder");
        String supplierId = request.getParameter("supplierId");

        // 调用服务层进行商品查询
        List<Goods> goodsList = goodsService.searchGoods(categoryId, numberOrder, shelfLifeOrder, priceOrder, supplierId);

        // 将商品列表存储在请求属性中
        request.setAttribute("goodsList", goodsList);
        request.setAttribute("supplierList", supplierService.getAllSupplier());
        request.setAttribute("categoryList", supplierService.getAllCategory());
        // 设置主页面的右侧内容页面
        request.setAttribute("mainRight", "/jsp/goodsSelect.jsp");
        // 转发请求到主页面
        request.getRequestDispatcher("/jsp/index.jsp").forward(request, resp);
    }
}
